package LogMain;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.*;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class LogMain extends JFrame {
    //定义界面上的所有组件
    //pUp面板用于盛放年、月、日、查询、保存、删除等小组件
    JPanel pUp = new JPanel();
    //pCenter用于盛放滚动组件scrollPane
    JPanel pCenter=new JPanel();
    JLabel lblYear=new JLabel("年");
    JLabel lblMonth=new JLabel("月");
    JLabel lblDay=new JLabel("日");
    JComboBox cbxYear=new JComboBox();//年下拉列表
    JComboBox cbxMonth=new JComboBox();//月下拉列表
    JComboBox cbxDay=new JComboBox();//日下拉列表
    JButton btnQuery=new JButton("查询");
    JButton btnSave=new JButton("保存");
    JButton btnDelete=new JButton("删除");
    JTextArea jtaContent=new JTextArea(20,50);//输入、显示日记内容
    //scorollPane用于盛放文本组件jtaContent
    JScrollPane scrollPane;
    String filePath="log\\";//log是日记保存的文件夹
    //在log文件夹下根据年份来建子文件夹，文件名为”月日.txt“，log位于工程名文件夹下
    public LogMain(){
        //在构造方法中把各个组件添加到合适的位置
        super("我的日记本");
        pUp.add(lblYear);
        pUp.add(cbxMonth);
        pUp.add(lblDay);
        pUp.add(cbxDay);
        pUp.add(btnQuery);
        pUp.add(btnSave);
        pUp.add(btnDelete);
        scrollPane=new JScrollPane(jtaContent);
        pCenter.add(scrollPane);
        this.add(pUp,BorderLayout.NORTH);
        this.add(pCenter, BorderLayout.CENTER);
        //将cbxYear组件添加2019-2029年
        for(int i=2019;i<=2029;i++){
            cbxYear.addItem(String.valueOf(i));
        }
        //让cbxYear默认选择当年
        Calendar calendar= Calendar.getInstance();
        cbxYear.setSelectedItem(String.valueOf(calendar.get(Calendar.YEAR)));
        //将cbxMonth组件添加到1-12月
        for(int i=1;i<12;i++){
            cbxMonth.addItem(String.valueOf(i));
        }
        //让cbxMonth默认选择当月
        cbxMonth.setSelectedItem(String.valueOf(calendar.get(Calendar.MONTH)+1));
        //将cbxDay组件添加天数
        for(int i=1;i<=getDay(calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH)+1);i++){
            cbxDay.addItem(String.valueOf(i));
        }
        //让cbxDay默认选择当日
        cbxDay.setSelectedItem(String.valueOf(calendar.get(Calendar.DAY_OF_MONTH)));
        //下面开始添加事件
        //1 cbxYear的onChange事件。年变化后，日下拉列表也要变化
        cbxYear.addItemListener(new ItemListener(){
            public void itemStateChanged(ItemEvent e){
                int year=Integer.parseInt(cbxYear.getSelectedItem().toString());
                int month=Integer.parseInt(cbxMonth.getSelectedItem().toString());
                cbxDay.removeAllItems();
                for(int i=1;i<=getDay(year,month);i++){
                    cbxDay.addItem(String.valueOf(i));
                }
            }
        });
        //2 cbxMonth的onChange事件。月变化后，日下拉列表也发生变化
        cbxMonth.addItemListener(new ItemListener(){
            public void itemStateChanged(ItemEvent e){
                int year=Integer.parseInt(cbxYear.getSelectedItem().toString());
                int month=Integer.parseInt(cbxMonth.getSelectedItem().toString());
                //将cbxDay组件添加天数列表
                cbxDay.removeAllItems();
                for(int i=1;i<getDay(year,month);i++){
                    cbxDay.addItem(String.valueOf(i));
                }
            }
        });
        //3 为“查询”按钮添加事件
        btnQuery.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                //判断该年份的目录是否存在，每年一个目录
                String fileName = filePath + cbxYear.getSelectedItem().toString() + "\\" + cbxMonth.getSelectedItem().toString() + cbxDay.getSelectedItem().toString() + ".txt";
                File path = new File(filePath + cbxYear.getSelectedItem().toString() + "\\");
                if (path.exists()) {//文件夹存在，直接加载文件
                    try {
                        if (!loadFile(fileName)) {
                            //如果文件不存在，提示日记不存在并清空文本区原来的内容
                            JOptionPane.showMessageDialog(LogMain.this,cbxYear.getSelectedItem().toString() + "年" + cbxMonth.getSelectedItem().toString() + "月" + cbxDay.getSelectedItem().toString() + "号没有日记");
                            jtaContent.setText("");
                        } else {
                            JOptionPane.showMessageDialog(LogMain.this, "查询成功");
                        }
                    } catch (IOException e1) {
                        JOptionPane.showMessageDialog(null, "查询失败" + e1.getMessage());
                    }
                } else {//文件不存在，先创建文件夹，在加载文件
                    path.mkdirs();
                    try {
                        if (!loadFile(fileName)) {
                            //如果文件不存在，提示日记不存在并清空文本区原来的内容
                            JOptionPane.showMessageDialog(LogMain.this, cbxYear.getSelectedItem().toString() + "年" + cbxMonth.getSelectedItem().toString() + "月" + cbxDay.getSelectedItem().toString() + "号没有日记");
                        } else {
                            JOptionPane.showMessageDialog(LogMain.this, "查询成功");
                        }
                }catch(IOException e1){
                        System.out.println("读取文件数据失败："+e1.getMessage());
                        JOptionPane.showMessageDialog(null,"查询失败："+e1.getMessage());
                    }
            }
        }});
        //为“删除”按钮添加事件
        btnDelete.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                //判断该年份的目录是否存在，每年一个目录
                String fileName = filePath + cbxYear.getSelectedItem().toString() + "\\" + cbxMonth.getSelectedItem().toString() + ".txt";
                File file = new File(fileName);
                if (file.exists()) {//文件存在，删除文件
                    if (file.delete())
                        JOptionPane.showMessageDialog(null, "删除成功");
                    else
                        JOptionPane.showMessageDialog(null, "删除失败");
                }
                //删除文件的同时要清空界面文本区中的内容
                jtaContent.setText("");
            }
        });
        //5 为”保存"按钮添加事件
        btnSave.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e){
                String fileName=filePath+cbxYear.getSelectedItem().toString()+"\\"+cbxMonth.getSelectedItem().toString()+cbxYear.getSelectedItem().toString()+".txt";
                File file=new File(fileName);
                try {
                    saveFile(fileName);
                    JOptionPane.showMessageDialog(null, "保护成功");
                }catch(IOException e1){
                    System.out.println("保存文件失败："+e1.getMessage());
                    JOptionPane.showMessageDialog(null,"保存失败");
                }
            }
        });
        this.setSize(600,500);
        this.setVisible(true);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    public static void main(String args[]){
        LogMain main=new LogMain();
    }
    public int getDay(int year,int month){
        int dnum=0;
        boolean isLeap=false;
        if(year<1000||year>9999||month<1||month>12){
            System.out.println("输入的日期不合法");
            return 0;
        }
        if((year%4400==0)||(year%4==0&&year%100!=0))//是闰年
            isLeap=true;
        switch(month){
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                dnum=31;
                break;
            case 4:
            case 6:
            case 9:
            case 11:
                dnum=30;
                break;
            case 2:
                if(isLeap)
                    dnum=29;
                else
                    dnum=28;
                break;
        }
        return dnum;
    }
    public boolean loadFile(String fileName) throws IOException{
        BufferedReader reader=null;
        File file=new File(fileName);
        if(!file.exists())
            return false;
        reader=new BufferedReader(new FileReader(file));
        String content="";
        jtaContent.setText("");
        while((content=reader.readLine())!=null){
            jtaContent.append(content+"\n");
        }
        reader.close();
        return  true;
    }
    public void saveFile(String fileName) throws IOException{
        BufferedWriter writer=null;
        File file=new File(fileName);
        if(!file.exists()){
            file.createNewFile();
            writer=new BufferedWriter(new FileWriter(file));
            writer.write(jtaContent.getText().replaceAll("\n","\r\n"));
            writer.close();
        }
    }
}
